package com.sun.lock;

import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 尝试获取锁 获取不到直接结束
 * @author SunAFei
 * @time 2018年3月13日 下午7:35:58
 * 
 */
public class TryLockDemo2 {
	private ArrayList<Integer> arrayList = new ArrayList<Integer>();
	private Lock lock = new ReentrantLock(); // 注意这个地方：lock 被声明为成员变量

	public static void main(String[] args) {
		final TryLockDemo2 test = new TryLockDemo2();

		new Thread("A") {
			public void run() {
				test.insert(Thread.currentThread());
			};
		}.start();
		// try {
		// Thread.sleep(2000);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		new Thread("B") {
			public void run() {
				test.insert(Thread.currentThread());
			};
		}.start();
	}

	public void insert(Thread thread) {
		if (lock.tryLock()) { // 使用 tryLock()
			try {
				System.out.println("线程" + thread.getName() + "得到了锁...");
				for (int i = 0; i < 5; i++) {
					arrayList.add(i);
				}
			} catch (Exception e) {

			} finally {
				System.out.println("线程" + thread.getName() + "释放了锁...");
				lock.unlock();
			}
		} else {
			System.out.println("线程" + thread.getName() + "获取锁失败...");
		}
	}
}

class MyThread1 extends Thread {
	private TryLockDemo2 test = null;

	public MyThread1(TryLockDemo2 test, String name) {
		super(name);
		this.test = test;
	}

	@Override
	public void run() {
		test.insert(Thread.currentThread());
	}
}
